package thread.timethreadpool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @auther zhouliangliang
 * @date 2022/7/25 11:54
 * @description 各种线程池的集合
 * 目前只有TimerThreadPoolExecutor，它可以记录线程执行的时间
 */
public class ThreadPools {
	private static Logger logger = LoggerFactory.getLogger(TimerThreadPoolExecutor.class);

	/**
	 * 可以记录线程执行的时间
	 */
	private static TimerThreadPoolExecutor timerThreadExecutor = new TimerThreadPoolExecutor(
		Runtime.getRuntime().availableProcessors(),
		Runtime.getRuntime().availableProcessors() * 2,
		1,
		TimeUnit.SECONDS,
		new ArrayBlockingQueue<>(4096),
		new CustomizableThreadFactory("Thread-"),
		new RejectedExecutionHandler() {
			@Override
			public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
				logger.error("Thread pool reject task.");
			}
		}
	);

	public static ThreadPoolExecutor getTimerThreadPool() {
		return timerThreadExecutor;
	}
}
